package com.youkeyi.ddy.cloud.application.business.hr.job;

import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.contract.HrEmployeeContractCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.contract.dto.HrEmployeeContractListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.contract.dto.HrEmployeeContractListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.contract.param.HrEmployeeContractQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.employee.HrEmployeeCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.employee.dto.HrEmployeeListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.employee.dto.HrEmployeeListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.employee.param.HrEmployeeQueryCloudParam;
import com.youkeyi.ddy.cloud.common.utils.SystemDateUtils;
import com.youkeyi.ddy.cloud.common.wx.applet.message.service.WxMessageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * EmployeeContractRemindJob
 *
 * @Author liuxiawang
 * @Date 2024/6/29 3:40 PM
 * @Location shenzhen.china
 */
@Slf4j
@Component
public class EmployeeContractRemindJob {

    @Autowired
    private WxMessageService wxMessageService;
    @Autowired
    private HrEmployeeCloudService hrEmployeeCloudService;
    @Autowired
    private HrEmployeeContractCloudService hrEmployeeContractCloudService;

    /**
     * 规定定时任务执行频率： 每天的09:07:00执行
     *
     * 合同快到期的第30、15、7、当天，四次提醒，发微信订阅消息
     */
    // @Scheduled(cron = "0 7 9 * * ?")
    private void employeeContractRemind() {
        log.info("员工合同提醒");

        HrEmployeeContractQueryCloudParam employeeContractQueryCloudParam = new HrEmployeeContractQueryCloudParam();
        employeeContractQueryCloudParam.setEndDateBegin(SystemDateUtils.localDateTime2Long(LocalDateTime.of(LocalDate.now(), LocalTime.MIN)));
        employeeContractQueryCloudParam.setEndDateEnd(SystemDateUtils.localDateTime2Long(LocalDateTime.of(LocalDate.now().plusDays(30), LocalTime.MAX)));
        HrEmployeeContractListCloudDto contractDtos = hrEmployeeContractCloudService.query(employeeContractQueryCloudParam);
        if (null != contractDtos && CollectionUtils.isNotEmpty(contractDtos.getRows())) {

            List<String> employeePrimaryKeys = contractDtos.getRows().stream().map(HrEmployeeContractListItemCloudDto::getEmployeeKey).collect(Collectors.toList());
            HrEmployeeQueryCloudParam employeeQueryCloudParam = new HrEmployeeQueryCloudParam();
            employeeQueryCloudParam.setEmployeePrimaryKeys(employeePrimaryKeys);
            HrEmployeeListCloudDto employeeDtos = hrEmployeeCloudService.query(employeeQueryCloudParam);
            Map<String, HrEmployeeListItemCloudDto> employeeMap = new HashMap<>();
            if (null != employeeDtos && CollectionUtils.isNotEmpty(employeeDtos.getRows())) {
                employeeMap.putAll(employeeDtos.getRows().stream().collect(Collectors.toMap(HrEmployeeListItemCloudDto::getPrimaryKey, Function.identity(), (k1, k2) -> k1)));
            }

            contractDtos.getRows().forEach(contractDto -> {
                // 判断当前合同的结束时间是不是距离现在30、15、7、当天的
                Long epochDay = SystemDateUtils.getEpochDays(contractDto.getEndDate(), System.currentTimeMillis());
                if (30 == epochDay || 15 == epochDay || 7 == epochDay || 0 == epochDay) {
                    String name = employeeMap.get(contractDto.getEmployeeKey()).getName();
                    wxMessageService.subscribeSendForContract(
                            new ArrayList<>(), // todo 人还没有确定给谁发
                            0 != epochDay ? String.format("%sd合同%d天后就到期了", name, epochDay) : String.format("%s的合同今天就到期了", name),
                            contractDto.getContractName(),
                            contractDto.getEmployeeContractPrimaryKey(),
                            contractDto.getStartDate(),
                            contractDto.getEndDate()
                    );
                }
            });

        }
    }

}
